package com.lxy.cxcyxmmsbackend.sys.exceptionHandle;


import com.lxy.cxcyxmmsbackend.sys.exceptionHandle.exce.LoginException;
import com.lxy.cxcyxmmsbackend.sys.exceptionHandle.exce.NormalException;
import com.lxy.cxcyxmmsbackend.sys.exceptionHandle.exce.PermissionException;
import com.lxy.cxcyxmmsbackend.sys.res.Result;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

@ControllerAdvice
public class ExceHandler {

    @Resource
    Result result;

    @ExceptionHandler(NormalException.class)
    @ResponseBody
    public Result normalExceptionHandle(Throwable throwable) {
        return Result.error(throwable.getMessage());
    }

    @ExceptionHandler(LoginException.class)
    @ResponseBody
    public Result loginExceptionHandle(Throwable throwable) {
        result.setCode("402");
        result.setMsg(throwable.getMessage());
        return result;
    }

    @ExceptionHandler(PermissionException.class)
    @ResponseBody
    public Result PermissionExceptionHandle(Throwable throwable) {
        result.setCode("401");
        result.setMsg(throwable.getMessage());
        return result;
    }

    @ExceptionHandler(Throwable.class)
    @ResponseBody
    public Result defaultExceptionHandle(Throwable throwable) {
        result.setCode("500");
        result.setMsg(throwable.getMessage());
        return result;
    }
}
